<?php

namespace App;

use App\Scopes\StatusScope;
use App\Traits\TimeFormat;
use Illuminate\Database\Eloquent\Model;

class Item extends Model {
	use TimeFormat;

	protected $fillable = [
		'title', 'user_id', 'cate_id', 'briefly', 'reprint', 'image', 'content', 'top', 'allow_comment', 'type', 'status',
	];

	protected $casts = [
		'status' => 'boolean',
		'allow_comment' => 'boolean',
		'top' => 'boolean',
	];

	public static function boot() {
		parent::boot();
		static::addGlobalScope(new StatusScope());
	}

	public function cate() {
		return $this->belongsTo(Cate::class)->select('id', 'cate_name');
	}

	public function tags() {
		return $this->belongsToMany(Tag::class, 'tagables')->select('tag_id', 'tag_name');
	}

	public function approves() {
		return $this->belongsToMany(User::class, 'approves')->select('user_id', 'nick_name', 'name', 'avatar');
	}

	public function comments() {
		return $this->hasMany(Comment::class)->select('id', 'reply_id', 'item_id', 'status', 'content', 'user_id', 'favor', 'created_at');
	}
}
